package com.yedean.factory.io;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ClassUtil;

import java.io.FileNotFoundException;
import java.io.InputStream;

/**
 * ClasspathResource
 * 类路径下的资源
 * 
 * @author yedean
 * @date 2022/7/7
 */
public class ClasspathResource implements Resource{
    
    //提供路径
    private String path;
    
    //提供类加载器
    private ClassLoader classLoader;


    public ClasspathResource(String path) {
        this(path,ClassUtil.getClassLoader());
    }
    

    public ClasspathResource(String path, ClassLoader classLoader) {
        this.path = path;
        Assert.notNull(path,"Path must not be null");
        this.classLoader = classLoader==null?classLoader: ClassUtil.getClassLoader();
    }

    @Override
    public InputStream getInputStream() throws FileNotFoundException {
        InputStream is = classLoader.getResourceAsStream(path);
        if (is ==null){
            throw new FileNotFoundException("找不到这个资源或者你写错了");
        }
        return is;
    }
}
